/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.jspx.txweb.view;

import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.env.TXWeb;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.util.PhotoString;
import com.jspx.txweb.util.RequestUtil;
import com.jspx.utils.NumberUtil;
import com.jspx.utils.RandomUtil;
import com.jspx.utils.StringUtil;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.awt.*;

/**
 * Created by chenyuan on 14-5-7.
 *
 * 表达式方式验证
 *
 *
 */
@HttpMethod(caption = "验证计算图片")
public class ValidateExpImgView extends ActionSupport {

    public ValidateExpImgView() {

    }

    // 图片的宽度。
    private int width = 70;
    // 图片的高度。
    private int height = 24;
    // 验证码干扰线数

    private String bgColor = "#FFFFFF";
    private String color = null;

    private String fileType = "png";
    private boolean safe = true;


    public int getWidth() {
        return width;
    }

    @Safety
    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    @Safety
    public void setHeight(int height) {
        this.height = height;
    }

    public String getBgColor() {
        return bgColor;
    }

    @Safety
    public void setBgColor(String bgColor) {
        this.bgColor = bgColor;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Safety(request = false)
    public void setSafe(boolean safe) {
        this.safe = safe;
    }

    @Safety(request = false)
    public void setFileType(String fileType) {
        this.fileType = fileType;
    }


    public String execute() throws Exception {
        if (safe&&RequestUtil.isPirated(request))
        {
            return NONE;
        }
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/" + fileType);
        Integer A = RandomUtil.getRandomInt(1,11);
        Integer B = RandomUtil.getRandomInt(1,11);
        Integer C;
        String viewCode = "";
        if (A>B)
        {
            C = A - B;
            viewCode = NumberUtil.toString(A) + "-" + NumberUtil.toString(B) + "=";
        } else if (A<9&&B<9||A.equals(B))
        {
            C = A * B;
            viewCode = NumberUtil.toString(A) + "X" + NumberUtil.toString(B) + "=";
        } else {
            C = A + B;
            viewCode = NumberUtil.toString(A) + "+" + NumberUtil.toString(B) + "=";
        }

        PhotoString validateCode = new PhotoString(width, height,new Color(Integer.parseInt(StringUtil.trim(StringUtil.replace(bgColor, "#", "")),16)), StringUtil.isNULL(color)?null:new Color(Integer.parseInt(StringUtil.trim(StringUtil.replace(color,"#","")),16)), viewCode);
        ServletOutputStream out = response.getOutputStream();
        try {
            ImageIO.write(validateCode.getBufferImage(), fileType, out);
            session.setAttribute(TXWeb.jspxValidate, NumberUtil.toString(C));
            session.setAttribute(TXWeb.jspxValidateTime, System.currentTimeMillis());
            out.flush();
        } finally {
            if (out!=null) out.close();
        }
        return NONE;
    }
}